﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.Coolzon.DesignServer
{
    class DataSourceConverter
    {
        public static Coolzon.Metadata.DataSource Convert(CPPEI.Coolzon.DesignServer.Domain.Models.PublishedDataSource mDataSource)//数据源转换
        {
            Coolzon.Metadata.DataSource dataSource = new Metadata.DataSource();
            dataSource.Name = mDataSource.DataSource.Name;
            dataSource.Caption = mDataSource.DataSource.Caption;
            dataSource.Type = mDataSource.DataSource.Type;
            dataSource.Version = new Version(mDataSource.DataSource.PublishedVersion);
            dataSource.RuntimeVersion = new Version(mDataSource.DataSource.RTVersion);
            dataSource.CacheMode = Coolzon.Metadata.DataSource.TryParseCacheMode("Share");
            dataSource.Address = mDataSource.DataSource.Address;

            dataSource.Authentication = new Metadata.Authentication();
            dataSource.Authentication.User = mDataSource.DataSource.AuthenticationUser;
            dataSource.Authentication.Mode = mDataSource.DataSource.AuthenticationMode;
            dataSource.Authentication.Password = mDataSource.DataSource.AuthenticationPassword;

            dataSource.DataSets = new List<Metadata.DataSet>();
            //data set
            foreach (var mDataSet in mDataSource.DataSets)
            {
                Coolzon.Metadata.DataSet dataSet = new Metadata.DataSet();
                dataSet.CacheMode = Coolzon.Metadata.DataSource.TryParseCacheMode("Share");
                dataSet.Caption = mDataSet.DataSet.Caption;
                dataSet.Name = mDataSet.DataSet.Name;
                dataSet.Type = mDataSet.DataSet.Type;
                dataSet.ValueString = mDataSet.DataSet.ValueString;
                dataSet.Parameters = new List<Metadata.Parameter>();

                // parameters
                foreach (var mParameter in mDataSet.DataParameters)
                {
                    Coolzon.Metadata.Parameter parameter = new Metadata.Parameter();

                    parameter.Caption = mParameter.Caption;
                    parameter.Name = mParameter.Name;
                    parameter.ValueType = mParameter.ValueType;

                    dataSet.Parameters.Add(parameter);
                }

                dataSource.DataSets.Add(dataSet);
            }

            return dataSource;
        }
    }
}
